<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_grade extends CI_Controller {
	function C_grade(){
		parent::__construct();
		if($this->auth->is_logged_in() == false){
			redirect('c=c_login','refresh');
		}
		$this->load->model('m_grade', '', TRUE);
	}
	
	public function index(){
		$this->load->view('main/v_grade');
	}
	
	function get_list(){
		$result=$this->m_grade->get_list();
		echo $result;
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "CREATE":
				$this->create();
				break;
			case "UPDATE":
				$this->update();
				break;
			case "DELETE":
				$this->delete();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	function create(){
		$grade=trim(@$_POST["grade"]);
		$grade=htmlentities($grade,ENT_QUOTES);
		$keterangan=trim(@$_POST["keterangan"]);
		$keterangan=htmlentities($keterangan,ENT_QUOTES);
		
		$result=$this->m_grade->create($grade, $keterangan);
		echo $result;
	}
	
	function update(){
		$grade_lama=trim(@$_POST["grade_lama"]);
		$grade_lama=htmlentities($grade_lama,ENT_QUOTES);
		$grade=trim(@$_POST["grade"]);
		$grade=htmlentities($grade,ENT_QUOTES);
		$keterangan=trim(@$_POST["keterangan"]);
		$keterangan=htmlentities($keterangan,ENT_QUOTES);
		
		$result=$this->m_grade->update($grade_lama, $grade, $keterangan);
		echo $result;
	}
	
	function delete(){
		$grade=trim(@$_POST["grade"]);
		$grade=htmlentities($grade,ENT_QUOTES);
		
		$result=$this->m_grade->delete($grade);
		echo $result;
	}
	
	function export_excel(){
		$rows=@$_POST["rows"];
		$this->firephp->log($rows);
		foreach($rows[0] as $field=>$value){
			$this->firephp->log($field);
		}
		$this->load->library('excel');//Panggil Library Excel
		
		$this->excel->setActiveSheetIndex(0);
		
		//set column name
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, 'GRADE');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(1, 1, 'KETERANGAN');
		
		//set records
		for($i=0; $i<sizeof($rows); $i++){
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $rows[$i]['GRADE']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $rows[$i]['KETERANGAN']);
		}
		$this->excel->getActiveSheet()->setTitle('grade_list');
		
		$this->excel->setActiveSheetIndex(0);
		
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
		
		$objWriter->save("./documents/grade.xlsx");    //Simpan sebagai apalah.xlsx*/
		
		echo 1;
	}
	
	function export_pdf(){
		$rows = isset($_POST['rows']) ? @$_POST['rows'] : @$_GET['rows'];
		/*$this->load->library('dom_pdf');
		$rows = isset($_POST['rows']) ? @$_POST['rows'] : @$_GET['rows'];
		$this->firephp->log($rows);
		
		$data["rows"] = $rows;
		
		$content = "main/p_grade";
		$filename = "grade";
		$orientation = "potrait";
		
		$html = $this->load->view($content,$data,TRUE);//Save as variable
		$this->dom_pdf->pdf_create($html, $filename);*/
		
		$this->load->library('cezpdf');
		$this->load->helper('pdf');
		prep_pdf();
		
		if(sizeof($rows) > 0){
			for($i=0; $i<sizeof($rows); $i++){
				$db_data['grade'] = array('grade' => $rows[$i]['GRADE'], 'keterangan' => $rows[$i]['KETERANGAN']);
			}
		}else{
			$db_data['grade'] = array('grade' => '', 'keterangan' => '');
		}
		
		$col_names = array(
			'grade' => 'GRADE',
			'keterangan' => 'KETERANGAN'
		);
		
		$this->cezpdf->ezTable($db_data, $col_names, 'Grade List', array('width'=>550));
		$this->cezpdf->ezStream();
	}
	
	function cetak(){
  		$rows = isset($_POST['rows']) ? @$_POST['rows'] : @$_GET['rows'];
		
		$data["rows"] = $rows;
		
		$print_view=$this->load->view("main/p_grade.php",$data,TRUE);
		if(!file_exists("documents")){
			mkdir("documents");
		}
		$print_file=fopen("documents/gradelist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */